var ProductGroupAdd = function() {
	var handleValidation = function() {
		var formAdd = $('#addForm');
		var validator = formAdd.validate({
			rules : {
				name : {
					required : true,
					rangelength : [ 1, 50 ],
					remote: {
                        url: ctx + "/productGroup/check",
                        type: "post",
                        dataType: "json",
                        data:{type:1}
                    }
				}
			},
            messages: {
            	name: {remote:"组别名称已被使用！"}
            },
			success : function(label, element) {
				var icon = $(element).parent('.input-icon').children('i');
				$(element).closest('.form-group').removeClass('has-error')
						.addClass('has-success'); // set success class to the
			},
			submitHandler : function(form) {
				var obj = $('form#addForm').serializeObject();
				$('#addModel').attr('disabled',true);
				$.ajax({
					type: "post",
					url: ctx + "/productGroup/doAdd",
					data: obj,
					success: function(msg) {
						if (msg.code == 1) {
							bootbox.dialog({
								message: "添加组别成功！",
								title: "温馨提示：",
								buttons: {
									main: {
										label: "确定",
										className: "btn btn-primary",
										callback: function() {
											window.location.href = 'productGroup/list';
										}
									}
								}
							});
							$('#addModel').attr('disabled',false);
						} else {
							bootbox.dialog({
								message: msg.message,
								title: "温馨提示：",
								buttons: {
									main: {
										label: "确定",
										className: "btn btn-primary",
										callback: function() {
											
										}
									}
								}
							});
							$('#addModel').attr('disabled',false);
						}
					},
					error: function(msg) {
						bootbox.alert("添加组别失败！");
						$('#addModel').attr('disabled',false);
					}
				});
			}
		});
	}

	return {
		init : function() {
			handleValidation();
		}
	};
}();